using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

// a comment
namespace Emlyn.Applications.Console.VersionNumberAdder
{
    class Program
    {
        static void Main(string[] args)
        {
            bool lneedUsage = true;
            try
            {
                if (args.Length == 2)
                {
                    string lVersionNumber = GetVersionNumber(args[1]);
                    (new System.IO.FileInfo(args[0])).MoveTo(args[0].Replace("#vernum#", lVersionNumber));
                    lneedUsage = false;
                }
            }
            catch (Exception ex)
            {
                System.Console.Out.WriteLine(ex.ToString());
            }

            if (lneedUsage)
            {
                System.Console.Out.WriteLine(String.Empty);
                System.Console.Out.WriteLine("Version Number Adder v" + Assembly.GetExecutingAssembly().GetName().Version.ToString());
                System.Console.Out.WriteLine("by Emlyn O'Regan (http://emlynoregan.com)");
                System.Console.Out.WriteLine(String.Empty);
                System.Console.Out.WriteLine("Usage: vername <Filename> <VersionBinary>");
                System.Console.Out.WriteLine("  <Filename>: Should include the substring #vernum# to ");
                System.Console.Out.WriteLine("              be replaced with a version number");
                System.Console.Out.WriteLine("  <VersionBinary>: A .net binary that the version number ");
                System.Console.Out.WriteLine("              can be read from");
                System.Console.Out.WriteLine(String.Empty);
            }
        }

        private static string GetVersionNumber(string p)
        {
            Assembly lassembly;

            try
            {
                lassembly = Assembly.LoadFrom(p);
            }
            catch
            {
                lassembly = Assembly.LoadFrom(System.IO.Path.GetFullPath(p));
            }

            return lassembly.GetName().Version.ToString();
        }
    }
}
